package com.db.person.controller;

import com.db.person.bean.Employee;
import com.db.person.bean.Status_inf;
import com.db.person.bean.UserVisit;
import com.db.person.bean.User_inf;
import com.db.person.service.IEmployeeService;
import com.db.person.service.IStatusInfService;
import com.db.person.service.IUserService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IStatusInfService statusInfService;

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/user/list")
    public String toList(Model model, PageModel pageModel){

        //查询所有的 再进行分页功能
        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());
        List<User_inf> all = userService.getAll();

        PageInfo<User_inf> pageInfo = new PageInfo<>(all);//得到分页相关的信息

        long total = pageInfo.getTotal();//总条数
        pageModel.setRecordCount((int)total);

        List<User_inf> list = pageInfo.getList();//得到当前页的数据

        model.addAttribute("list",list);//展示的数据
        model.addAttribute("pageModel",pageModel);//前端需要分页的参数
        model.addAttribute("hre","user/list");//分页跳转的路径

        model.addAttribute("str1","已审核");
        model.addAttribute("str2","未审核");

        return "user/list";
    }

    @RequestMapping("/user/toadd")
    public String toAdd(Model model){

        //查询employee 根据user_id = null  返回给新增页面
        List<Employee> employees = employeeService.getEmployeesAndUserIdIsNull();
        model.addAttribute("emp_list",employees);
        return "user/add";
    }

    @RequestMapping("/user/add")
    public String add(User_inf user_inf){
        //注册之前要给密码进行加密
        SimpleHash simpleHash = new SimpleHash("MD5",user_inf.getPassword(),user_inf.getLoginname(),1024);
        String realPwd = simpleHash.toString();
        user_inf.setPassword(realPwd);//让对象里的密码改为加密后的密码
        userService.regist(user_inf);

        //更新员工表的user_id
        Integer user_id = user_inf.getId();
        Integer emp_id = user_inf.getEmpId();
        employeeService.updateUserId(user_id,emp_id);

        return "redirect:/user/list";//使用重定向
    }

    @RequestMapping("/user/toedit")
    public String toEdit(Integer id,Model model){

        //根据id查询对应的数据
        User_inf user = userService.getById(id);

        List<Status_inf> all = statusInfService.getAll();

        model.addAttribute("user",user);
        model.addAttribute("status_list",all);
        return "user/edit";
    }

    @RequestMapping("/user/edit")
    public String edit(User_inf user_inf){

        userService.setSomeById(user_inf);

        return "redirect:/user/list";
    }

    @RequestMapping("/user/delete")
    @ResponseBody
    public String delete(Integer id){

        try {
            // 根据id 调用service 调用 mapper 进行数据库中的删除
            userService.deleteById(id);
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "no";
        }

    }

}
